package cn.dnui_zll731.dnuifood_731zlll.model;
import cn.dnui_zll731.dnuifood_731zlll.bean.Result;
import cn.dnui_zll731.dnuifood_731zlll.common.Constants;
import cn.dnui_zll731.dnuifood_731zlll.bean.UserBean;
import cn.dnui_zll731.dnuifood_731zlll.iface.UserIface;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.service.UserService;
import retrofit2.Call;
public class UserModel extends BaseModel implements UserIface {
    private UserService service;
    public UserModel(){
        //通过LoginListener返回数据。
        this.service = retrofit.create(UserService.class);
    }
    @Override
    public void login(String username, String userpass, final RetrofitListener listener) {
        //使用Retrofit----2创建请求。
        Call<UserBean> call = service.doLogin(username,userpass);
        bindCallback(call,listener, Constants.USERLOGIN);
        //使用Retrofit----3发送请求。
//        call.enqueue(new Callback<UserBean>() {
//            @Override
//            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
//
//                if(response.isSuccessful() && response.body()!=null)
//                {
//                    listener.onSuccess(response.body(),1);
//                }
//                else   listener.onFalure("用户名或者密码错误");
//            }
//
//            @Override
//            public void onFailure(Call<UserBean> call, Throwable t) {
//                    listener.onFalure("网络解析错误");
//            }
//
//        });
    }

    @Override
    public void register(String username, String userpass, String mobilenum, String address, String comment, final RetrofitListener listener) {
        Call<UserBean> call =service.doRegister(username,userpass,mobilenum,address,comment);
        bindCallback(call,listener,Constants.USERREGIISTER);
    }

    @Override
    public void userCollectFood(String user_id, String food_id, RetrofitListener listener) {
        Call<Result> call = service.userCollectFood(user_id,food_id);
        bindCallback(call,listener,Constants.USERCOLLECTFOOD);
    }

    @Override
    public void getUserById(String user_id, RetrofitListener listener) {
        Call<UserBean> call = service.getUserById(user_id);
        bindCallback(call,listener,Constants.GETUSERBYID);
    }

    @Override
    public void insertOrder(String user_id, String food_id, String num, String sum, String suggesttime, String address, RetrofitListener listener) {
        Call<Result> call = service.insertOrder(user_id,food_id,num,sum,suggesttime,address);
        bindCallback(call,listener,Constants.INSERTORDER);
    }

    @Override
    public void getAllUserCollection(String user_id, String flag, RetrofitListener listener) {
        Call call = service.getAllUserCollection(user_id,flag);
        bindCallback(call,listener,Constants.GETALLUSERCOMMENT);
    }

    @Override
    public void updateUserById(String user_id, String username, String userpass, String mobilenum, String address, RetrofitListener listener) {
        Call call = service.updateUserById(user_id,username,userpass,mobilenum,address);
        bindCallback(call,listener,Constants.UPDATEUSERBYID);
    }

    @Override
    public void getAllCommentsByUser(String user_id, RetrofitListener listener) {
        Call call = service.getAllCommentsByUser(user_id);
        bindCallback(call,listener,Constants.GETALLCOMMENTSBYUSER);
    }

    @Override
    public void getAllOrdersByUser(String user_id, RetrofitListener listener) {
        Call call = service.getAllOrdersByUser(user_id);
        bindCallback(call,listener,Constants.GETALLORDERSBYUSER);
    }

    @Override
    public void updateComment(String item_id, String content, RetrofitListener listener) {
        Call call = service.updateComment(item_id,content);
        bindCallback(call,listener,Constants.UPDATECOMMENT);
    }

    @Override
    public void deleteComment(String item_id, RetrofitListener listener) {
        Call call = service.deleteComment(item_id);
        bindCallback(call,listener,Constants.DELETECOMMENT);
    }

    @Override
    public void insertComment(String item_id, String content,RetrofitListener listener) {
        Call call = service.insertComment(item_id,content);
        bindCallback(call,listener,Constants.INSERTCOMMENT);
    }


}
